<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\Role;
use App\Models\Supplier;
use App\Models\UserLoginLog;
use App\Models\Users;
use App\Services\GoodsOrderService;
use App\Services\SchoolService;
use App\Services\SupplierService;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class SupplierController extends AdminController
{
    //添加
    public function add(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'logo' => 'required|string',
            'username' => 'required|string',
            'password' => 'required|string',
            'pid' => 'required|integer',
            'category' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $validator = Validator::make($request->all(), [
            'phone' => 'regex:/^1[345789][0-9]{9}$/',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_PHONE_UNREGISTERED);
        }
        $supplier =  SupplierService::getInstance()->getByUsresname($request->username);
        if(!is_null($supplier)){
            return $this->fail(CodeResponse::AUTH_NAME_REGISTERED);
        }
        $supplier =  SupplierService::getInstance()->getByPhone($request->phone);
        if(!is_null($supplier)){
            return $this->fail(CodeResponse::AUTH_PHONE_REGISTERED);
        }
        $supplier = new Supplier();
        $supplier->name = $request->name;
        $supplier->logo= $request->logo;
        $supplier->register_capital= $request->register_capital;
        $supplier->register_address= $request->register_address;
        $supplier->production_address= $request->production_address;
        $supplier->description= $request->description;
        $supplier->category= $request->category;
        $supplier->pid= $request->pid;
        $supplier->status= 0;
        $supplier->username= $request->username;
        $supplier->password= Hash::make($request->password);
        $supplier->phone= $request->phone;
        $supplier->save();
        return $this->success($supplier->id);

    }
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer|digits_between:1,20|min:1',
//            'name' => 'required|string',
//            'logo' => 'required|string',
//            'username' => 'required|string',
//            'password' => 'required|string',
//            'pid' => 'required|string',
//            'category' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $validator = Validator::make($request->all(), [
            'username' => 'unique:suppliers,username,' . $request->id . ',id',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_NAME_REGISTERED);
        }

        $validator = Validator::make($request->all(), [
            'phone' => 'unique:suppliers,phone,' . $request->id . ',id',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_PHONE_REGISTERED);
        }
        $supplier = Supplier::find($request->id);

        $supplier->name = $request->name;
        $supplier->logo= $request->logo;
        if(isset($request->register_capital)){
            $supplier->register_capital= $request->register_capital;
        }
        if(isset($request->register_address)){
            $supplier->register_address= $request->register_address;
        }
        if(isset($request->production_address)){
            $supplier->production_address= $request->production_address;
        }
        if(isset($request->description)){
            $supplier->description= $request->description;
        }
        $supplier->category= $request->category;
        $supplier->pid= $request->pid;
        $supplier->status= 0;
        $supplier->username= $request->username;
        $supplier->password= Hash::make($request->password);
        $supplier->phone= $request->phone;
        $supplier->save();
        return $this->success($supplier->id);

    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'category' => 'required|integer',
            'pid' => 'required|integer',
            'name' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $category = $request->input('category');
        $pid = $request->input('pid');
        $name = $request->input('name');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $list = SupplierService::getInstance()->list($page,$limit,$sort,$order,$category, $pid,$name);
        return $this->success($list);
    }
    //供应商信息
    public function  info(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $id = $request->input('id');
        $info = SupplierService::getInstance()->info($id);

        return $this->success($info);
    }
    //未被供应商选择的学校
    public function notAllocationSchool(Request $request){
        $validator = Validator::make($request->all(), [
            'supplier_id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $list = SupplierService::getInstance()->notAllocationSchool($request->supplier_id);
        return $this->success($list);
    }
    //分配学校给供应商
    public function allocationSchool(Request $request){
        $validator = Validator::make($request->all(), [
            'supplier_id' => 'required|integer',
            'school_id' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $supplier_id = $request->supplier_id;
        $schoolArr = $request->school_id;
        $schoolArr = is_array($schoolArr) ? $schoolArr : json_decode($schoolArr,true);
        if(!is_array($schoolArr) || count($schoolArr) < 1){
            return $this->fail(CodeResponse::PARAM_ILLEGAL,'学校id不能为空');
        }
        SupplierService::getInstance()->allocationSchool($supplier_id,$schoolArr);
        return $this->success();
    }
    //分配给普通供应商的订单列表
    public function  supplierOrderlist(Request $request){
        $validator = Validator::make($request->all(), [
            'supplier_id' => 'integer',
            'order_num' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $supplier_id = $request->input('supplier_id');
        $order_num = $request->input('order_num');
        $username = $request->input('username');
        $order_type = $request->input('order_type');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $list = GoodsOrderService::getInstance()->supplierOrderlist($page,$limit,$sort,$order,$supplier_id, $order_num,$username,$order_type);
        return $this->success($list);
    }
    //未分配给供货商的游客订单列表
    public function  touristSupplierOrderlist(Request $request){
        $validator = Validator::make($request->all(), [
            'order_num' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $order_num = $request->input('order_num');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $list = GoodsOrderService::getInstance()->touristSupplierOrderlist($page,$limit,$sort,$order,$order_num);
        return $this->success($list);
    }

    //分配游客订单给供应商
    public function allocationOrder(Request $request){
        $validator = Validator::make($request->all(), [
            'supplier_id' => 'required|integer',
            'order_num' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $supplier_id = $request->supplier_id;
        $order_num = $request->order_num;
        $orderArr = SupplierService::getInstance()->getSupplierOrder($supplier_id,$order_num);
        if(count($orderArr) > 0){
            return $this->fail(CodeResponse::SUPPLIER_GOODS_ORDER_EXIST);
        }
        $id= SupplierService::getInstance()->allocationOrder($supplier_id,$order_num,1);
        return $this->success($id);
    }
    //超级供应商的未分配订单列表
    public function  unallocatedSupplierOrderlist(Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $order_num = $request->input('order_num');
        $username = $request->input('username');
        $order_type = $request->input('order_type');

        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $list = GoodsOrderService::getInstance()->unallocatedSupplierOrderlist($page,$limit,$sort,$order,$order_num,$username,$order_type);
        return $this->success($list);
    }
    //删除
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $goodsBrand = Supplier::find($request->id);
        $goodsBrand->delete();
        return $this->success();
    }

    // 上传结果
    public function img(Request $request)
    {
        $url = config('filesystems.disks.supplier.url');
        $default = config('filesystems.disks.supplier.default');
        $file = SupplierService::getInstance()->upfile($request, $url, $default, 'supplier');
        return $this->success([
                'file' => $file,
            ]
        );
    }
    public function supplierLogin(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'username' => 'required',
            'password' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $supplier =  SupplierService::getInstance()->getByUsresname($request->username);
        if(is_null($supplier)){
            return $this->fail(CodeResponse::AUTH_INVALID_ACCOUNT);
        }
       $bool = auth('supplier')->attempt($request->all());
        if (!$bool) {
            return $this->fail(CodeResponse::AUTH_INVALID_ACCOUNT, '账号密码不对');
        }
        //获取token
//        $token = Auth::guard('admin')->login($supplier);
        $token = null;
        //更新登录的信息
        $supplier->last_login_time = now()->toDateTimeString();
        $supplier->last_login_ip = $request->getClientIp();
        $supplier->last_session = Session()->getId();
        if (!$supplier->save()) {
            return $this->fail(CodeResponse::UPDATED_FAIL);
        }
        //组装数据并返回
        return $this->success([
            'token' => $token,
            'userInfo' => [
                'supplier_id' => $supplier->id,
                'name' => $supplier->username,
                'logo' => $supplier->logo,
                'category' => $supplier->category,
                'avatarUrl' => $supplier->pic,
                'loginTime' => $supplier->last_login_time,
            ]
        ]);
    }

    public function logout(Request $request){
        auth('supplier')->logout();
        return $this->success();
    }
}
